package com.wfit.controller.admin;

import com.wfit.dto.CateDTO;
import com.wfit.dto.CatePageVO;
import com.wfit.po.Cate;
import com.wfit.result.Result;
import com.wfit.service.CateService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;

@Api(tags = "商品分类")
@RestController
@RequestMapping("/cate")

public class CateController {
    @Autowired
    private CateService cateService;

    /**
     * 商品分类列表
     * @return
     */
    @ApiOperation("商品分类列表")
    @GetMapping
    public Result list() {
        Result result = cateService.listCate();

       return result;
    }

    /**
     * 查询单个分类
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result findById(@PathVariable Integer id) {
        Cate byId = cateService.getById(id);

        return Result.success(byId);
    }

    /**
     * 获取该分类下的子分类
     * @param id
     * @return
     */
    @GetMapping("/childeren/{id}")
    public Result getChilderen(@PathVariable Integer id) {
        if (id == -1) {
            return Result.success();
        }
        Result result = cateService.getChilderen(id );

        return result;
    }

    /**
     * 插入新分类
     * @param dto
     * @return
     */
    @PostMapping
    public Result insert( CateDTO dto) throws IOException {
        cateService.insertCate(dto);
        return Result.success();
    }

    /**
     * 更新分类
     * @param dto
     * @return
     */
    @PutMapping
    public Result update( CateDTO dto) throws IOException {
        Result result = cateService.updateCate(dto);
        return result;

    }
    /**
     * 新增子分类
     * @param dto
     * @return
     */
    @PostMapping("/{pId}")
    public Result saveCh( CateDTO dto,@PathVariable Long pId) throws IOException {
        Result result = cateService.saveCh(dto, pId);
        return Result.success();

    }

    /**
     * 根据分类Id删除分类
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable Integer id) {
        cateService.removeById(id);
        return Result.success();
    }

    /**
     * 根据查询条件查询数据
     * @param dto
     * @return
     */
    @PostMapping("/search")
    public Result search(@RequestBody CatePageVO dto) {
        Result search = cateService.search(dto);
        return Result.success(search);
    }
}
